<?php
use Doctrine\Common\ClassLoader,
	Doctrine\ORM\Configuration,
	Doctrine\ORM\EntityManager,
	Doctrine\Common\Cache\ArrayCache,
	Doctrine\DBAL\Logging\EchoSQLLogger;

class Doctrine {

	public $em = null;
	private $sqlLoggingEnabled = false;
	
	public function __construct()
	{
		// load database configuration from CodeIgniter
		require_once APPPATH.'config/database.php';

		// Set up class loading. You could use different autoloaders, provided by your favorite framework,
		// if you want to.
		require_once SYSDIR.'/libraries/Doctrine/Common/ClassLoader.php';

		$doctrineClassLoader = new ClassLoader('Doctrine',  SYSDIR.'/libraries');
		$doctrineClassLoader->register();
		$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
		$entitiesClassLoader->register();
		$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
		$proxiesClassLoader->register();

		// Set up caches
		$config = new Configuration;
		$cache = new ArrayCache;
		$config->setMetadataCacheImpl($cache);
		$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
		$config->setMetadataDriverImpl($driverImpl);
		$config->setQueryCacheImpl($cache);

		$config->setQueryCacheImpl($cache);

		// Proxy configuration
		$config->setProxyDir(APPPATH.'/models/proxies');
		$config->setProxyNamespace('Proxies');

		// Set up logger
		$logger = new EchoSQLLogger;
		$config->setSQLLogger($logger);

		$config->setAutoGenerateProxyClasses( TRUE );
		// Database connection information
		$connectionOptions = array(
			'driver' => 'pdo_mysql',
			'user' =>     $db[$active_group]['username'],
			'password' => $db[$active_group]['password'],
			'host' =>     $db[$active_group]['hostname'],
			'dbname' =>   $db[$active_group]['database']
		);

		// Create EntityManager
		$this->em = EntityManager::create($connectionOptions, $config);
	}

	public function toggleSQLLogging() {
		$this->sqlLoggingEnabled = !$this->sqlLoggingEnabled;
		$this->em->getConfiguration()->getSQLLogger()->enabled = $this->sqlLoggingEnabled;
	}
}
